<?php


namespace app\admin\controller;


use app\admin\model\Node;
use app\admin\model\User;
use think\Db;
use think\facade\View;
use think\response\Json;

class IndexController extends Base
{

    /**
     * 菜单列表 树状
     *
     * @return string
     */
    public function index(Node $node): string
    {

        // 获取权限菜单
        View::assign([
            'menu' => $node->getMenu(session('rule'))
        ]);

        return View::fetch('/index');
    }

    /**
     * 后台默认首页
     * @return mixed
     */
    public function indexPage(User $userModel): string
    {
        // 生成从 8点 到 22点的时间数组
        $dateLine = array_map(function ($vo) {
            if ($vo < 10) {
                return '0' . $vo;
            } else {
                return $vo;
            }
        }, range(8, 22));

        // 初始化数据
        $line = [];
        foreach ($dateLine as $key => $vo) {
            $line[$vo] = [
                'is_talking' => intval(rand(20, 120)),
                'in_queue'   => intval(rand(0, 20)),
                'success_in' => intval(rand(50, 200)),
                'total_in'   => intval(rand(150, 300))
            ];
        }

        $showData = [];
        foreach ($line as $key => $vo) {
            $showData['is_talking'][] = $vo['is_talking'];
            $showData['in_queue'][]   = $vo['in_queue'];
            $showData['success_in'][] = $vo['success_in'];
            $showData['total_in'][]   = $vo['total_in'];
        }

        $user = $userModel->where('id', session('id'))->find()->toArray();
        View::assign('user_info', $user);
        View::assign('address', $user['last_login_ip']);

        return View::fetch('index');
    }

    public function timing_right(): Json
    {
        $data = Db::name("comments")->where(array('comments_status' => 0))->order("create_time DESC")->select();
        foreach ($data as $k => $vo) {
            $data[$k]['create_time'] = date("Y-m-d H:i:s", $vo['create_time']);
        }
        echo json_encode($data);
    }

}